<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use App\User;
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $users = DB::table('users')->paginate(2);
        return view('admin.user_index')->with('users',$users);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
    	return view('admin.user_add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
    	$this->validate($request,[
    			'name'=>'required',
    			'email'=>'required',
    			'mobile'=>'required',
    			'password'=>'required', 
		    	]);
		    	$user['name']=$request->name;
		    	$user['email']=$request->email;
		    	$user['mobile']=$request->mobile;
                $user['created_at']  = date("Y-m-d H:i:s");
                $user['updated_at']  = date("Y-m-d H:i:s");
		    	$user['password']=$request->password;
		    	DB::table('users')->insert($user);
		    	return redirect('/user');
	
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
    	$user = DB::table("users")->where("id",$id)->first();
    	//$userInfo = DB::table('user_infos')->where('uid',$id)->first();
        return view('admin.user_show')->with('user',$user);
        //->with('userInfo',$userInfo);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
    	$user = User::find($id);
        return view("admin.user_edit")->with('user',$user);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
    	$user = DB::table('users')->where('id',$id)
           ->update(['name'=>$request->name,'mobile'=>$request->mobile,'email'=>$request->email,'password'=>$request->password]);
    	
    	return redirect('/user');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    /**
     * @param  [type] $id
     *删除
     * @return [type]
     */
    public function delete($id)
    {
        $user=DB::table('users')->where('id',$id)->delete();
        
        	return 1;
       
        	
    }
}
